//
//  MessageDetailViewController.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/9/25.
//

import UIKit

class MessageDetailViewController: BaseViewController {
    var tableView: BaseTableView!
    var massagesId = ""
    var model: MessageListModel!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.initTableView()
        self.requestGetMassagesDetail()
    }
    
    func initTableView(){
        self.tableView = BaseTableView(frame: CGRect(x: 0, y: NavBarHight, width: kScreenWidth, height: kScreenHeight - NavBarHight), style: .grouped)
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.rowHeight = UITableViewAutomaticDimension
        self.tableView.register(MessageDetailTitleCell.self, forCellReuseIdentifier: "MessageDetailTitleCell")
        self.tableView.register(MessageDetailRichTextCell.self, forCellReuseIdentifier: "MessageDetailRichTextCell")
        self.tableView.estimatedRowHeight = 2
        self.view.addSubview(self.tableView)
        
    }

    //MARK: - Request
    //站内讯息详情
    func requestGetMassagesDetail(){
        self.view.showLoadingHUDText()
        let param = ["massagesId": self.massagesId]
        XTRequestCenter.shared.sendRequestMappableTargetObject(target: APIManager.massagesDetail(param: param),objectType: MessageDetailModel.self) { data in
            let resultObj = data as! MessageDetailModel
            self.view.handleLoading()
            if resultObj.code == "1" {
                self.model = resultObj.data
            }
            
            self.tableView.reloadData()
        }error: { err in
            self.view.handleLoading()
            self.view.makeToast(message: err?.domain ?? MMLocalizedString("request_failed", comment: "请求失败，请稍候再试"))
        }
    }
    
    
}

extension MessageDetailViewController: UITableViewDelegate,UITableViewDataSource{
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
       
        return UITableViewAutomaticDimension
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch indexPath.section {
        case 0:
            let cell = tableView.dequeueReusableCell(withIdentifier: "MessageDetailTitleCell", for: indexPath) as! MessageDetailTitleCell
            if self.model != nil{
                cell.setModel(model: self.model)
            }
           return cell
        default:
            let cell = tableView.dequeueReusableCell(withIdentifier: "MessageDetailRichTextCell", for: indexPath) as! MessageDetailRichTextCell
            if self.model != nil{
                cell.setModel(model: self.model)
            }
//            weak var weakSelf = self
//            cell.webViewHeightBlock = { (height: CGFloat) in
//                weakSelf?.tableView.reloadData()
//            }
            
           return cell
        }
       
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 0.01))
          headerView.backgroundColor = .clear
        return headerView
    }
    
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let footerView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 0.01))
        footerView.backgroundColor = .clear
        return footerView
    }
    
}
